<?php

namespace Act\Logic\Event;

use Act\Logic\Event\CallBackInterface;
use \common\Table\InputClick\AdClickMidiTable;
use \common\Table\InputClick\MidiEffectiveClickTable;
use \common\Factory\Factory;
use \core\data\partitionTable;

class MidiCallback implements CallBackInterface {
    private function selectMidiByIdfa($appid, $idfa, $eventTimestamp, $tableNum) {
        $config = \core\init\init::getConfig();
        $maxSecond = $config['actMaxSecond'];
        $adClickMidiTable = new AdClickMidiTable();
        $result = $adClickMidiTable->selectByIdfa($appid, $idfa, $eventTimestamp, $maxSecond, $tableNum);

        return $result === false ? false : $result;
    }
    
    private function curlCallback($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, urldecode($url));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        Factory::getServer('MqWriteLog')->send('midi curl callback url:'. $url .' | result:'. json_encode($output));
        curl_close($ch);
    }
    
    public function actChange($appid, $idfa, $eventTimestamp) {
        $callback = false;
        $partitionTable = new partitionTable();
        
        strlen($eventTimestamp) == 13 && $eventTimestamp = substr($eventTimestamp, 0, 10);
        $tableNum = $partitionTable->getHashTable($idfa);
        
        //查询此次激活是否是有效激活（点击后1个月内激活为有效激活）
        $queryIdfaResult = $this->selectMidiByIdfa($appid, $idfa, $eventTimestamp, $tableNum);
        
        $result = false;
        if($queryIdfaResult !== false) {
            $callback = $queryIdfaResult['callback'];
            $midiEffectiveClickTable = new MidiEffectiveClickTable();
            //查询数据是否已存在
            $effectiveSelectResult = $midiEffectiveClickTable->selectByIdfaAndAppId($idfa, $appid);
            
            if($effectiveSelectResult === false) {
                $effectiveResult = $midiEffectiveClickTable->insertEffectiveMidi(array('idfa' => $idfa, 'appid' => $appid, 'click_timestamp' => $queryIdfaResult['input_timestamp'], 'act_timestamp' => $eventTimestamp, 'time' => date('Ym', $queryIdfaResult['input_timestamp'])));
                if($effectiveResult === true) {
                    $result = true;
                    $this->curlCallback($callback);
                    Factory::getServer('MqWriteLog')->send('act insert database midi effective and collect success');
                }
                else {
                    Factory::getServer('MqWriteLog')->send('act insert database midi effective failed');
                }
            }
            else {
                $result = true;
                Factory::getServer('MqWriteLog')->send('act insert database midi effective appid:'. $appid .' idfa:'. $idfa .' is exists');
            }
        }
        else {
            $result = true;
        }
        
        return $result;
    }
}